using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Security.Permissions;
using needle.UI.Controls.Flat.Utilities;
using needle.UI.Controls.Flat.Utilities.Win32;

namespace needle.UI.Controls.Flat.Windows.Forms
{
	internal class DatePickerPopUp : WPopUpFormBase
	{
		private MonthCalendar monthCalendar;
		private System.ComponentModel.IContainer components = null;
		protected Control mparent = null;

		[Category("Behavior")]
		public event DateSelectionChangedEventHandler SelectionChanged = null;
		
		#region Constructors

		public DatePickerPopUp(Control parent,DateTime date) : base(parent)
		{
			mparent = parent;
			InitializeComponent();
			monthCalendar.SetDate(date);

			if (this.mparent is ImageDatePicker)
			{
				monthCalendar.TitleBackColor = ((ImageDatePicker)this.mparent).CalendarTitleBackColor;
				monthCalendar.TitleForeColor = ((ImageDatePicker)this.mparent).CalendarTitleForeColor;
				monthCalendar.TrailingForeColor = ((ImageDatePicker)this.mparent).CalendarTrailingForeColor;
				monthCalendar.BackColor = ((ImageDatePicker)this.mparent).CalendarBackColor;
				monthCalendar.ForeColor = ((ImageDatePicker)this.mparent).CalendarForeColor;
			}
		}
		
		#endregion

		#region Dispose
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
            this.monthCalendar = new needle.UI.Controls.Flat.Windows.Forms.MonthCalendar();
            this.SuspendLayout();
            // 
            // monthCalendar
            // 
            this.monthCalendar.Location = new System.Drawing.Point(2, 2);
            this.monthCalendar.Name = "monthCalendar";
            this.monthCalendar.ShowToday = false;
            this.monthCalendar.ShowTodayCircle = false;
            this.monthCalendar.TabIndex = 0;
            this.monthCalendar.KeyUp += new System.Windows.Forms.KeyEventHandler(this.EnterpriseDatePickerPopUpKeyUp);
            this.monthCalendar.MouseUp += new System.Windows.Forms.MouseEventHandler(this.monthCalendar1_MouseUp);
            // 
            // DatePickerPopUp
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(231, 167);
            this.Controls.Add(this.monthCalendar);
            this.Name = "DatePickerPopUp";
            this.Text = "DatePickerPopUp";
            this.DoubleClick += new System.EventHandler(this.EnterpriseDatePickerPopUpDoubleClick);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.EnterpriseDatePickerPopUpKeyUp);
            this.ResumeLayout(false);

		}
		#endregion
		
		#region Events handlers
		private void EnterpriseDatePickerPopUpKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyData == Keys.Escape)
			{
				this.Close();
			}

			if(e.KeyData == Keys.Enter)
			{
				OnSelectionChanged();
				this.Close();
			}
		}

		private void EnterpriseDatePickerPopUpDoubleClick(object sender, System.EventArgs e)
		{
			System.Windows.Forms.MonthCalendar.HitTestInfo hTest = monthCalendar.HitTest(this.PointToClient(Control.MousePosition));
			System.Windows.Forms.MonthCalendar.HitArea hArea = hTest.HitArea;

			if(hArea == System.Windows.Forms.MonthCalendar.HitArea.Date)
			{
				monthCalendar.SelectionStart = hTest.Time;
				OnSelectionChanged();

				this.Close();
			}
		}

		private void monthCalendar1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			System.Windows.Forms.MonthCalendar.HitTestInfo hTest = monthCalendar.HitTest(this.PointToClient(Control.MousePosition));
			System.Windows.Forms.MonthCalendar.HitArea hArea = hTest.HitArea;

			if(hArea == System.Windows.Forms.MonthCalendar.HitArea.Date)
			{
				monthCalendar.SelectionStart = hTest.Time;
				OnSelectionChanged();

				this.Close();
			}
		}

		#endregion

		#region Virtuals

		protected void OnSelectionChanged()
		{
			if(this.SelectionChanged != null)
			{
				this.SelectionChanged(this,new DateChangedEventArgs(monthCalendar.SelectionStart));
			}
		}
		
		#endregion

		#region Overrides
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			base.OnPaint(e);

			if (this.mparent is ImageDatePicker)
			{
				Rectangle rect = new Rectangle(this.ClientRectangle.Location,new Size(this.ClientRectangle.Width - 1,this.ClientRectangle.Height - 1));
				
				using(Pen pen = new Pen(((ImageDatePicker)this.mparent).BorderHotColor))
				{
					e.Graphics.DrawRectangle(pen,rect);
				}
			}
		}

		public override void PostMessage(ref Message m)
		{
			Message msg = new Message();
			msg.HWnd    = monthCalendar.Handle;
			msg.LParam  = m.LParam;
			msg.Msg     = m.Msg;
			msg.Result  = m.Result;
			msg.WParam  = m.WParam;

			monthCalendar.PostMessage(ref msg);
		}

		#endregion

		#region Properties

		public DateTime SelectedDate
		{
			get {return this.monthCalendar.SelectionEnd;}
		}

		#endregion
	}
}
